package com.patchmanage.back.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.patchmanage.back.annotation.OperLog;
import com.patchmanage.back.entity.PatchUpdateConfig;
import com.patchmanage.back.result.Result;
import com.patchmanage.back.service.IPatchUpdateConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huizhao
 * @since 2021-03-31
 */
@Api(value = " 补丁更新管理 controller", tags = {"补丁更新管理 Api"})
@RestController
@RequestMapping("/back/patchUpdateConfig")
public class PatchUpdateConfigController {


    @Autowired
    public IPatchUpdateConfigService patchUpdateConfigService;

    /**
     * 获取补丁接口
     *
     * @param dict
     * @return
     */
    @ApiOperation(value = "获取补丁更新接口", notes = "获取补丁更新接口")
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer", paramType = "body")
    })
    public Result<PatchUpdateConfig> getById(@ApiIgnore @RequestBody Dict dict) {
        return Result.success(patchUpdateConfigService.getById(dict.getInt("id")));
    }


    @ApiOperation(value = "新增补丁更新接口", notes = "新增补丁更新接口")
    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
    @OperLog(operModul="补丁更新",operDesc="新增补丁更新")
    public Result<Boolean> saveOrUpdate(@RequestBody PatchUpdateConfig patchUpdateConfig){
        if(StrUtil.isNotBlank(patchUpdateConfig.getAutoConfig())){
            JSONObject jsonObject = JSONUtil.parseObj(patchUpdateConfig.getAutoConfig());
            Date date = jsonObject.getDate("time");
            jsonObject.set("time", DateUtil.format(date,"HH:mm:ss").toString());
            patchUpdateConfig.setAutoConfig(jsonObject.toString());
        }
        return Result.success(patchUpdateConfigService.saveOrUpdate(patchUpdateConfig));
    }
}
